package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.ApplicationForSupervisorDao;
import cn.edu.sdjzu.xg.bysj.domain.ApplicationForSupervisor;
import lombok.extern.slf4j.Slf4j;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;

@Slf4j
public class ApplicationForSupervisorService {
    //获取 ApplicationForSupervisorDao实例
    ApplicationForSupervisorDao applicationForSupervisorDao = ApplicationForSupervisorDao.getInstance();
    //新建ApplicationForSupervisorService对象
    private static ApplicationForSupervisorService applicationForSupervisorService = new ApplicationForSupervisorService();
    //构造器
    private ApplicationForSupervisorService(){}
    //获取ApplicationForSupervisorService对象
    public static ApplicationForSupervisorService getInstance(){
        return applicationForSupervisorService;
    }

    /**
     * 添加
     * @param applicationForSupervisor
     * @return
     * @throws SQLException
     */
    public boolean add(ApplicationForSupervisor applicationForSupervisor) throws SQLException {
        //获得数据库链接
        Connection connection = JdbcHelper.getConn();
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        boolean added = false;
        try {
            //添加志愿
            log.trace("开始添加");
            //添加
            added = applicationForSupervisorDao.add(connection,applicationForSupervisor);
            log.trace("添加完成");
            //提交到数据库
            connection.commit();
        } catch (SQLException e){
            //如有异常回滚添加的信息
            connection.rollback();
            //抛出数据库异常
            throw new SQLException(e.getMessage());
        } finally {
            //开启自动提交，事务结束
            connection.setAutoCommit(true);
            //关闭数据库链接
            JdbcHelper.close(connection);
        }
        return added;
    }
}
